package com.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class DateUtils {

    public static Date beginOfDay() {
        return beginOfDay(null);
    }

    public static Date beginOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        if ( date!=null ) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static Date endOfDay() {
        return endOfDay(null);
    }

    public static Date endOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        if ( date!=null ) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static Date addDay(Date date, int amount) {

        Calendar calendar = Calendar.getInstance();
        if ( date!=null ) {
            calendar.setTime(date);
        }
        calendar.add(Calendar.DAY_OF_MONTH, amount);
        return calendar.getTime();
    }

}
